package test
{
	import flash.net.URLRequest;
	import flash.utils.clearInterval;
	import flash.utils.setInterval;
	
	import level4.display.StatisticsLoader;

	public class StatisticsLoaderTest
	{
		public function StatisticsLoaderTest()
		{
			var url:String = "http://www.wuxi-thw.com/software/%e7%8e%8b%e8%8f%b2-%e4%bc%a0%e5%a5%87.mp3";
			
			statistics([url,url]);
		}
		
		private function statistics(urls:Array):void{
			//总长度
			var totalLen:uint = 0;
			//当前总下载量
			var totalLoading:uint = 0;
			//下载个数
			var count:int = 0;
			//开始加载
			for(var i:int = 0;i<urls.length;i++)
				startLoad(urls[i]);
			//测试 长度
			testTotalLen();
			//////////////////
			//闭包
			//////////////////
			function testTotalLen():void{
				var timerId:int = setInterval(function():void{
					if(count >= urls.length-1){
						clearInterval(timerId);
						trace("总长度:",totalLen);
					}
				},50);
			}
			
			function startLoad(url:String):void{
				var l:StatisticsLoader = new StatisticsLoader();
					l.load(new URLRequest(url));
					//下载中,尝试获取 已下载的总长度
					l.setLoadingNotify(function(lenDiff:uint):void{
						totalLoading += lenDiff;
						trace("已下载总字节:",totalLoading);
					});
					//尝试 获取 总长度
					l.getTotalLenWhileCanGetIt(function(len:uint):void{
						totalLen += len;
						count++;
					});
			}
		}
	}
}